package com.itheima.service.impl;

import com.itheima.mapper.UserMapper;
import com.itheima.pojo.User;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private PasswordEncoder passwordEncoder;

    @Override
    public void insertUser(User user) {


        // 如果数据库已经存在相同用户名的用户，返回错误提示
        User existUser = userMapper.loadUserByUsername(user.getUsername());
        if (existUser != null) {
            throw new RuntimeException("用户已存在");
        }
        // 对密码加密
        user.setPassword(passwordEncoder.encode(user.getPassword()));
        // 保存用户
        userMapper.insertUser(user);

    }

    @Override
    public void login(User user) {
        userMapper.loadRolesByUserId(user.getId());
    }
}
